#include "stdafx.h"
#include "XmlConfigAdapter.h"
#include "Util.h"

bool CXmlConfigAdapter::ReadToMap
(SetMap &TempMap/*,
 SetList & TempList,
 SetList & NotDestroyItemList,
 SetList &NotMailItemList*/)
{
    bool bReturn = false;
    if (!CUtil::IsExitFile(m_strFileName))
    {
        return false;
    }

	HRESULT hr;
	MSXML2::IXMLDOMDocumentPtr pDoc;
    
	hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
    if (CO_E_FIRST==hr)
    {
        return false;
    }
    pDoc->load( _variant_t(m_strFileName));
 
	MSXML2::IXMLDOMElementPtr  pDocElement;
	MSXML2::IXMLDOMElementPtr  pParElement;

	pDocElement=pDoc->GetdocumentElement();
    if (pDocElement == NULL)
	{
	    return false;
	}
	

	if (pDocElement->hasChildNodes())
	{
		pParElement = pDocElement->selectSingleNode("item");
		
		if (pParElement == NULL)
		{
			pDoc->save(_variant_t(m_strFileName));
	        return false;
		}
	    
		MSXML2::IXMLDOMElementPtr pPos;
		MSXML2::IXMLDOMElementPtr pLastPos;
		if (pParElement->hasChildNodes())
		{
			CString strPosName, strLast; 
			CString strPosValue;
			
            pPos = pParElement->GetfirstChild();
			strPosName = (LPCTSTR)(_bstr_t)pPos->getAttribute(_T("name"));
	        pLastPos = pParElement->GetlastChild();
	        strLast = (LPCTSTR)(_bstr_t)pLastPos->getAttribute(_T("name"));

	        do
		    {
			   strPosValue = (LPCTSTR)(_bstr_t)pPos->getAttribute("value");
			  
               TempMap[strPosName] = strPosValue;
               
			   if (strPosName.Compare(strLast) == 0)
				   break;

			   if (pPos == NULL)
			   {
	               return false;
			   }

			   pPos = pPos->GetnextSibling();
			   strPosName = (LPCTSTR)(_bstr_t)pPos->getAttribute(_T("name"));
			   
			}while(1);
		}
    }

    return bReturn;
}

bool CXmlConfigAdapter::WriteToLacol
(
 SetMap &TempMap/*,
 SetList & TempList,
 SetList & NotDestroyItemList,
 SetList &NotMailItemList*/)
{
    return WriteToFile(TempMap);
}

bool CXmlConfigAdapter::WriteToFile(SetMap& TmpMap)
{
    if (m_strFileName.IsEmpty()) return false;
	HRESULT hr;
	MSXML2::IXMLDOMDocumentPtr pDoc;
	//const CLSID CLSID_DOMDocument;
	hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
    if(!SUCCEEDED(hr)) 
	{
		return (false); 
	} 

	MSXML2::IXMLDOMElementPtr  pDocElement;
	MSXML2::IXMLDOMElementPtr  pParElement;

	pDocElement=pDoc->createElement("settings");
    pDoc->appendChild(pDocElement);

	pParElement = pDoc->createElement("item");
	pDocElement->appendChild(pParElement);
	SetMap::iterator iterpos;
   

	for (iterpos = TmpMap.begin(); iterpos != TmpMap.end(); iterpos++)
	{
		if (iterpos->first == _T("CONFIG")) continue;
		MSXML2::IXMLDOMElementPtr pPos;
		pPos = pDoc->createElement("variant");
		
		CString strPosName, strPosValue;
		strPosName = iterpos->first;
		strPosValue = iterpos->second;
		
		pPos->setAttribute("name", _variant_t(strPosName));
		pPos->setAttribute("value", _variant_t(strPosValue));
		
		pParElement->appendChild(pPos);
	}
    pDoc->save(_variant_t(m_strFileName));
    return true;
}